<?php
namespace helper;

use Yii;
use yii\base\Object;
use proxy\HttpProxy;
use proxy\WeixinApiProxy;
            

//4，如果没有openid，则获取openid

class SessionHelper extends Object
{
    public $session = null;
    public $http_proxy = null;
    public $weixin_proxy = null;
    
    public $APPID = null; 
    public $GET_OPENID_URL = null; 
    public $APPSEC = null; 
    
    public function __construct(){
        $request = Yii::$app->request;
        $pc = isset($_COOKIE['pc']) ? $_COOKIE['pc'] : '0';
        if($pc != 1){
            ini_set("session.save_handler", "memcache");
            ini_set("session.save_path", "tcp://10.157.164.1:11211");
        }
        $this->session = Yii::$app->getSession();
        $this->http_proxy = new HttpProxy(); 
        
    }
    
    public function get($key){
        if($key){
            return $this->session->get($key);
        }else{
            return '';
        }
    }
    
    public function set($key, $value){
        if($key){
            return $this->session->set($key, $value);
        }else{
            return false;
        }
    }

    public function destroy() {
        $this->session->destroy();
    }
    
    public function check($auth){
        //测试环境，跳过session 
        $request = Yii::$app->request;
        $thisPath = "/".$request->getPathInfo(); 
        if(isset($this->session['admin_user_id']) && !empty($this->session['admin_user_id'])){
            if ($thisPath == "/") {
                return ['status' => 0]; // success, every one can access /
            }

            if($auth->checkAccess($this->session->get('admin_user_id'),$thisPath)) {
                return ['status' => 0]; // success
                //return true;
            } else {
                return ['status' => 2]; // no permission
                //header('Location:'.Yii::$app->params['HOST'].'/nopermission');
                //exit;
                //return false; //临时不检查权限，正式应该用上一行
            }
        }else{
            return ['status' => 1]; // not login
            //header('Location:'.Yii::$app->params['HOST'].'/login');
            //exit;
        }
    }

    
   

}
